﻿@model VVA.Data.Entity.Taxonomy

@using (Html.BeginForm(ViewContext.RouteData.Values["action"].ToString(), "Taxonomy", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmTaxonomy" }))
{
    Html.ValidationSummary();
    <fieldset>
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.TaxonomyID)
        <p>
            @Html.LabelFor(model => model.TaxonomyName)
            @Html.TextBoxFor(model => model.TaxonomyName, new { @class = "text-input medium-input" })
            @Html.ValidationMessageFor(model => model.TaxonomyName)
        </p>
        <p>
            @Html.LabelFor(model => model.Slug)
            @Html.TextBoxFor(model => model.Slug, new { @class = "text-input medium-input" })
            @Html.ValidationMessageFor(model => model.Slug)
        </p>
        <p>
            @Html.LabelFor(model => model.ParentID)
            @Html.DropDownListFor(model => model.ParentID, (SelectList)ViewBag.Taxonomy, "-- Chọn một danh mục --", new { @class = "medium-input" })
        </p>
        <p>
            @Html.LabelFor(model => model.Description)
            @Html.TextAreaFor(model => model.Description, new { @class = "text-input medium-input" })
        </p>
        <p>
            @Html.LabelFor(model => model.SortOrder)
            @Html.TextBoxFor(model => model.SortOrder, new { @class = "text-input medium-input numeric", type = "number" })
        </p>
        <p>
            @Html.LabelFor(model => model.Avatar)
            <img id="imgAvatar" src="@Model.Avatar" alt="@Model.TaxonomyName" />
            <input type="file" id="Avatar" />
        </p>
        <p>
            @if (ViewContext.RouteData.Values["action"].Equals("Create"))
            {
                <a id="btnSave" class="button" href="javascript:void(0);">@HttpContext.GetGlobalResourceObject("Common", "SaveAndAddNew")</a>    
            }
            <a id="btnSaveAndClose" class="button" href="javascript:void(0);">@HttpContext.GetGlobalResourceObject("Common", "Save")</a>
            @Html.RouteLink(HttpContext.GetGlobalResourceObject("Common", "BackToList").ToString(), "Category", new { action = "Index" }, new { @class = "button" })
        </p>
    </fieldset>    
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#frmTaxonomy").validate();

        $("#Avatar").kendoUpload({
            async: {
                saveUrl: "@Url.Action("Upload", "FileManagement")",
                saveField: "attachments",
                removeUrl: "remove",
                autoUpload: true
            },
            success: function (e) {
                $("#imgAvatar").attr("src", "@Url.Content("~/Upload/Image/")" + e.files[0].name);
            },
            showFileList: false
        });

        $("#btnSave").live("click", function () {
            if ($("#frmTaxonomy").valid()) {
                SaveTaxonomy(false);
            }
        });

        $("#btnSaveAndClose").live("click", function () {
            if ($("#frmTaxonomy").valid()) {
                SaveTaxonomy(true);
            }
        });
    });

        function SaveTaxonomy(backToList) {
            var model = {
                TaxonomyName: $("#TaxonomyName").val(),
                Slug: $("#Slug").val(),
                ParentID: $("#ParentID").val(),
                Description: $("#Description").text(),
                AppliedTable: "@ViewBag.TableName",
                TaxonomyId: $("#TaxonomyID").val(),
                SortOrder: $("#SortOrder").val(),
                Avatar: $("#imgAvatar").attr("src")
            }

            $.ajax({
                type: "POST",
                url: "@Url.Action(this.ViewContext.RouteData.Values["action"].ToString(), "Taxonomy")",
                data: model,
                error: function (message) {
                    alert(message);
                },
                success: function () {
                    alert("Cất dữ liệu thành công!!!");
                    if (backToList) {
                        window.location = "@Url.RouteUrl("Category", new { action = "Index" })";
                }
                else {
                    document.forms[0].reset();
                }
            }
            });
    }

</script>
